<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\common\model\SpecGoods;
use app\home\business\CartBusiness;
use think\exception\HttpException;
use think\facade\View;
use think\Request;

class Cart extends Base
{
    /**
     * 添加购物车、需要知道的是SKUid
     */
   public function addCart(){
       $sku_id = \request()->get('sku_id');
       $number = \request()->get('number');
       # 验证参数
       if (!is_numeric($sku_id) || !is_numeric($number)) {
           return redirect('/');
       }
        # 验证sku是否存在
       $skuInfo = SpecGoods::with('spu')->visible(['spu' =>['id','is_on_sale','goods_logo','goods_name']])->find($sku_id)->toArray();
       # 判断该商品是否上架、以及库存是否充足
       if ($skuInfo['spu']['is_on_sale']!=1) {
            return View::fetch('/goods/unsale-detail',['error' => '该商品已下柜，欢迎挑选其他商品！']);
       }

       if ($skuInfo['store_count'] < $number) {
           return View::fetch('/goods/unsale-detail',['error' => "该商品库存不足，欢迎挑选其他商品！"]);
       }
       if (!session("?userInfo")) {
           # 存储一下回调的路由
           cookie('redirect_url','http://pyg.com/detail.html?id='.$skuInfo['goods_id']);
           return  redirect('/login');
       }
       try {
           # 添加购物车
           CartBusiness::addCart($skuInfo,$number);

           # 加入购物车成功
           # 库存、在客户端不展示
           unset($skuInfo['store_count']);
           $skuInfo['number'] = $number;//添加购物车的数量
           return  View::fetch('/cart/success-cart',['skuInfo' => $skuInfo]);
       }catch (HttpException $httpException) {
           return View::fetch('/goods/unsale-detail',['error' => '加入购物车失败、请稍后重试']);
       }

   }

    /**
     * 购物车列表
     */
   public function cart(){
       $data = CartBusiness::getCart();
       View::assign('data',$data);
       return View::fetch();
   }

   /**
    * 修改购买数量
    */
   public function changeNumber(){
       try {
           $id = \request()->post('id');
           $number = \request()->post('number');
           if (!is_numeric($id) || !is_numeric($number)) {
               abort(2001,'参数不正确');
           }

           CartBusiness::changeNumber($id,$number);
            return  json(['code' => 200,'msg' =>'ok']);

       }catch (HttpException $httpException) {
           return json(['code' => $httpException->getStatusCode(),'msg' => $httpException->getMessage()]);
       }
   }

    /**
     * 修改选中状态
     *
     * @return \think\response\Json
     */
   public function changeSelected(){

       try {
           $id = \request()->post('id');
           $is_selected = \request()->post('is_selected');


           if (!is_numeric($id) || !is_numeric($is_selected)) {
               abort(2001,'参数不正确');
           }

           CartBusiness::changeSelected($id,$is_selected);
            return  json(['code' => 200,'msg' =>'ok']);

       }catch (HttpException $httpException) {
           return json(['code' => $httpException->getStatusCode(),'msg' => $httpException->getMessage()]);
       }
   }

   public function del(){
       try {
           $id = \request()->get('id');


           if (!is_numeric($id)) {
               abort(2001,'参数不正确');
           }

           CartBusiness::del($id);
           return  redirect('/cart');

       }catch (HttpException $httpException) {
           return "删除异常";
       }
   }

    /**
     * 批量删除
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|string|\think\response\Redirect
     */
   public function batchDel(){
       try {
           $id = \request()->post('ids');


           if (!is_numeric($id)) {
               abort(2001,'参数不正确');
           }

           CartBusiness::batchDel($id);
           return  json(['data' => [],'code' => 200,'msg' => 'ok']);

       }catch (HttpException $httpException) {
           return  json(['data' => [],'code' => $httpException->getStatusCode(),'msg' => $httpException->getMessage()]);
       }
   }
}
